package es.uclm.itsue.internet;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;


public class ShowIPActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        String DEVICEIP = getLocalIpAddress();
        Log.i("ShowIPActivity", "Device IP: " + DEVICEIP);
        Toast.makeText(this, "Device IP: " + DEVICEIP,
    			Toast.LENGTH_LONG).show();
        finish();
    }

    private String getLocalIpAddress() {
    	
        try {
        	Enumeration<NetworkInterface>
        	en = NetworkInterface.getNetworkInterfaces();
            while (en.hasMoreElements()) {
            	NetworkInterface intf = en.nextElement();
            	Enumeration<InetAddress> 
            	enumIpAddr = intf.getInetAddresses();
                while (enumIpAddr.hasMoreElements()) {
                	InetAddress
                	inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) { 
                    	return inetAddress.getHostAddress()
                    			.toString();
                    }
                }
            }
        } catch (SocketException ex) {
            Log.e("ShowIPActivity", ex.toString());
        }
        return null;
    }
}




/*

public boolean isConnected() 
{ 
    try{ 
        ConnectivityManager cm = (ConnectivityManager) getSystemService 
                                                    (Context.CONNECTIVITY_SERVICE); 
        NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
 
        if (netInfo != null && netInfo.isConnected()) 
        { 
            //Network is available but check if we can get access from the network. 
            URL url = new URL("http://www.Google.com/"); 
            HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); 
            urlc.setRequestProperty("Connection", "close"); 
            urlc.setConnectTimeout(2000); // Timeout 2 seconds. 
            urlc.connect(); 
 
            if (urlc.getResponseCode() == 200)  //Successful response. 
            { 
                return true; 
            }  
            else  
            { 
                 Log.d("NO INTERNET", "NO INTERNET"); 
                return false; 
            } 
        } 
    } 
    catch(Exception e) 
    { 
        e.printStackTrace(); 
    } 
    return false; 
} 


*/